<?php
/**
 *处理用户权限组与权限的对应关系
 */
class Adminuserpriv extends Base_Controller
{
    public function __construct()
    {
        /******待编辑******/
        $this->_page_size = 20;
        $this->_form_type = 'userpriv';
        $this->_list_where = array();
        $this->_action_list = array('EDIT','DEL');
        $this->_controller = 'admin/adminuserpriv/';
        parent::__construct();
    }

    public function showList($page = 1)
    {
        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/');

        $res = $this->getList($offset,$limit);

        $createbtn = $this->load->view('admin/form/createbtn',array('controller'=>site_url($this->_controller)),true);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],
            'pagination'=>$pdata['pagination'],'createbtn'=>$createbtn);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);

    }

    public function getList($offset = 0,$limit = 0)
    {
        $data = $this->db->select('r.id,g.privgrouptitle,p.privcontent')->from('mod_privillege_relations as r')
            ->join('mod_privillege_group as g','g.privgroupid=r.privgroupid','left')
            ->join('mod_privillege as p','p.privcontentid=r.privcontentid','left')
            ->order_by('r.id','DESC')
            ->get()->result_array();
        $res = array('headers'=>array(),'list'=>array());
        foreach($data as $row){
            $rowdata = array();
            $rowdata['privgrouptitle']=$row['privgrouptitle'];
            $rowdata['privcontent']=$row['privcontent'];
            $rowdata['actionlist']=$this->adminform_model->getActionList($this->_action_list,$this->_controller,$row['id'],0);
            $res['list'][]=$rowdata;
        }
        $res['headers']=array('权限组','权限','操作');
        return $res;
    }

    public function update($id = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['privgroupid'] = (int)$post['privgroupid'];
            $post['privcontentid'] = (int)$post['privcontentid'];
            $this->_update_data = $post;
            parent::update($id);
        }
    }

    public function create()
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['privgroupid'] = (int)$post['privgroupid'];
            $post['privcontentid'] = (int)$post['privcontentid'];

            $this->_create_data = $post;
            parent::create();
        }
    }

    public function delete($id = 0)
    {
        $this->delete($id);
    }

}